package demo.database.H2.D02_性能测试;


import java.sql.Connection;
import java.sql.PreparedStatement;

import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;

import demo.database.H2.utils.H2Utils;

public class T01_写入性能测试 {
    private static void run() throws Exception {
        run(100000);    // [3秒199毫秒] [用时(ms)=3199]
    }
    private static void run(int count) throws Exception {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = H2Utils.getConnection();
            
            DBUtils.execute(conn, "CREATE TABLE cc (id varchar(40) PRIMARY KEY, data int, create_dt datetime)");
            
            stmt = conn.prepareStatement("INSERT INTO cc VALUES (RANDOM_UUID(), ?, NOW())");
            for(int i = 0; i < count; i++){
                stmt.setInt(1, i);
                stmt.execute();
            }
            
            String value = DBUtils.getValue(conn, "select count(*) from cc");
            ConsoleUtils.log("value", value);
        } finally {
            DBUtils.close(stmt);
            DBUtils.close(conn);
            H2Utils.shutdown();
        }
    }    


    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print();
        }
    }
}
